/*
* @(#)ListUserRecordAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.workflow.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.dao.WorkflowGroupDAObject;

/**
 * <p>Class Name:       ListWorkflowGroupAction.java    </p>
 * <p>Description:      The list action class for ListWorkflowGroup.jsp</p>
 *
 * @author           Angus Shiu
 * @company          DCIVision Limited
 * @creation date    29/07/2003
 * @version          $Revision: 1.6 $
 */
public class ListWorkflowGroupAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.6 $";


  /** Creates a new instance of ListWorkflowGroupAction */
  public ListWorkflowGroupAction() {
    super();
    this.setListName("workflowGroupList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.workflow.dao.WorkflowGroupDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_GROUP);
  }
  
  /**
   *  Method execute() - generaic function of result list handling, including:
   *                     1. Delete record in List
   *                     2. Get List Data
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
	    if (form != null && GlobalConstant.OP_MODE_LIST_RETAIN.equals(((AbstractSearchForm)form).getOpMode())) {
	      // Retain the attributes of search form.
	      if ((ActionForm)request.getSession().getAttribute(this.m_sListName)!=null) {
	        form = (ActionForm)request.getSession().getAttribute(this.m_sListName);
	        request.setAttribute(mapping.getAttribute(), form);
	      } else {
	        request.getSession().setAttribute(this.m_sListName, form);
	      }
	    } else {
	      // Set the search from into session for remembering the settings.
	      request.getSession().setAttribute(this.m_sListName, form);
	    }

	    AbstractSearchForm searchForm = (AbstractSearchForm)form;
	    log.info(this.getClass().getName()+", OP Mode:"+searchForm.getOpMode()+", Nav Mode:"+searchForm.getNavMode());

	    // Set the function code for current action.
	    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
	    if (forward!=null) {
	      return forward;
	    }

	    if (GlobalConstant.OP_MODE_LIST_DELETE.equals(searchForm.getOpMode())) {
	      try {   
	    	  if(this.isContainerWorkflow(searchForm, request)){
	    		  addError(request, "errors.workflow.isContainerWf");
	    	  }else {  	 
	    		  this.deleteListData(mapping, searchForm, request, response);
	    		  this.commit(request);   
	    		  if (!Utility.isEmpty(searchForm.getBasicSelectedID())){
		    		  addMessage(request, MESSAGE_RECORDS_DELETED, ""+searchForm.getBasicSelectedID().length);
		    	  }
	    	  }  	  
	    	  searchForm.setBasicSelectedID(null);
	    	  searchForm.setOpMode(null);
	    	  
	    	  // Check for all item deletes per page
	    	  this.getListData(mapping, searchForm, request, response);           
	       } catch (ApplicationException e) {
	          this.rollback(request);
	          this.handleApplicationException(request, e);
	          request.setAttribute(this.m_sListName, new ArrayList());
	       }
	    } else {
	      try {
	        this.getListData(mapping, searchForm, request, response);
	      } catch (ApplicationException e) {
	        this.handleApplicationException(request, e);
	        request.setAttribute(this.m_sListName, new ArrayList());
	      }
	    }

	    List resultList = (List)request.getAttribute(this.m_sListName);
	    while ( (resultList.size()==0) && (TextUtility.parseInteger(searchForm.getCurStartRowNo())>TextUtility.parseInteger(searchForm.getPageOffset())) ) {
	      int prvRowNo = TextUtility.parseInteger(searchForm.getCurStartRowNo())-TextUtility.parseInteger(searchForm.getPageOffset());
	      searchForm.setCurStartRowNo(TextUtility.formatInteger(prvRowNo));
	      this.getListData(mapping, searchForm, request, response);
	    }

	    return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
	  }
  

  /**
   * the mother return a value that use judge WorkflowRecord,if container workflow.
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   */
  public boolean isContainerWorkflow(AbstractSearchForm form, HttpServletRequest request) {
    boolean result = false;
    try {
      AbstractDAObject dao = this.getMajorDAObject(request);
      WorkflowGroupDAObject wfdao = (WorkflowGroupDAObject) dao;
      String groupIDArr[] = form.getBasicSelectedID();
      int workflowCountID = wfdao.getContainerWorkflowCountByGroupID(groupIDArr);
      if (workflowCountID == 0) {
        result = false;
      } else {
        result = true;
      }
    } catch (ApplicationException e) {
      log.error(e);
    }
    return result;
  }


}
